from typing import List
class Solution:
    def nextPermutation(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead. 原地修改
        """
        flag = 0
        flag2 = 0
        fu2 = 0
        list2 = nums[::]
        for i in range(len(list2) - 1):
            if list2[i] > list2[i + 1]:
                flag = i + 1
                fu2 += 1
            else:
                flag = i
                flag2 = i
        if fu2 == flag == len(list2) - 1:
            list2.sort()
        else:
            if flag < len(list2) - 1:
                list2[flag], list2[flag + 1] = list2[flag + 1], list2[flag]
            else:
                list2[flag2], list2[flag2 + 1] = list2[flag2 + 1], list2[flag2]
        nums[:] = list2[:]
        print(nums)
    def nextPermutation2(self, nums: List[int]) -> None:
        flag = 0
        flag2 = 0
        list2 = nums[::-1]
        for n in range(len(list2) - 1):
            if list2[n] > list2[n + 1]:
                flag = n + 1
                break
        for o in range(len(list2)):
            if list2[o] > list2[flag]:
                flag2 = o
                break
        if flag == 0:
            nums.sort()
        else:
            nums[:] = nums[::-1]
            nums[flag], nums[flag2] = nums[flag2], nums[flag]
            nums[:flag] = sorted(nums[:flag], reverse=True)  # 降序排列
            nums[:] = nums[::-1]
lll = Solution()
lll.nextPermutation2([2, 3, 1])
